package com.lx.oms.controller.controller;

import java.math.BigInteger;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lx.oms.common.annotation.SysLog;
import com.lx.oms.common.controller.AbstractController;
import com.lx.oms.common.entity.Page;
import com.lx.oms.common.entity.R;
import com.lx.oms.common.utils.ImageOperation;
import com.lx.oms.controller.entity.InformationEntity;
import com.lx.oms.controller.service.InformationService;

/**
 * 资讯
 *
 * @author houzhong
 * @date 2018年7月19日 下午12:00:08
 */
@RestController
@RequestMapping("/sys/information")
public class InformationController extends AbstractController {
	
	@Autowired
	private InformationService informationService;
	
	/**
	 * 列表
	 * @param params
	 * @return
	 */
	@RequestMapping("/list")
	public Page<InformationEntity> list(@RequestBody Map<String, Object> params) {
		return informationService.listInformation(params);
	}
		
	/**
	 * 新增
	 * @param tInformation
	 * @return
	 */
	@SysLog("新增资讯")
	@RequestMapping("/save")
	public R save(@RequestBody InformationEntity information,String contentStr,HttpServletRequest request) {
		//目录
		String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage/");
		//解析base64位，压缩图片
		String yzmImagePath = ImageOperation.encode64Image(information.getImg(),url);
		//获取图片路径
		information.setImg(yzmImagePath);
		information.setContent(contentStr);
		return informationService.saveInformation(information);
	}
	
	/**
	 * 根据id查询详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/info")
	public R getById(@RequestBody String id) {
		BigInteger bid = new BigInteger(id);
		return informationService.getInformationById(bid);
	}
	
	/**
	 * 修改
	 * @param tInformation
	 * @return
	 */
	@SysLog("修改资讯")
	@RequestMapping("/update")
	public R update(@RequestBody InformationEntity information,String contentStr,HttpServletRequest request) {
		//目录
		String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage/");
		//解析base64位，压缩图片
		String yzmImagePath = ImageOperation.encode64Image(information.getImg(),url);
		//获取图片路径
		information.setImg(yzmImagePath);
		information.setContent(contentStr);
		return informationService.updateInformation(information);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@SysLog("删除资讯")
	@RequestMapping("/remove")
	public R batchRemove(@RequestBody String[] id) {
		return informationService.batchRemove(id);
	}
	
}
